package com.aeye.mbr.upms.dao.keygenerator;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.mapperhelper.SelectKeyGenerator;

import java.sql.Statement;

/**
 * Created by Administrator on 2018/4/2.
 */
public class AeyeSelectKeyGenerator implements KeyGenerator{
    private KeyGenerator generator = null;
    public AeyeSelectKeyGenerator(String type,MappedStatement keyStatement, boolean executeBefore) {
        if("SNOWFLAKE".equals(type)){
            this.generator = new SnowFlakeKeyGenerator(keyStatement,executeBefore);
        }else if(type.startsWith("AESEQ_")){
            this.generator = new TableSequenceKeyGenerator(type.substring(type.indexOf("_")+1),keyStatement,executeBefore);
        }else{
            this.generator = new SelectKeyGenerator(keyStatement,executeBefore);
        }
    }

    @Override
    public void processBefore(Executor executor, MappedStatement mappedStatement, Statement statement, Object o) {
        this.generator.processBefore(executor,mappedStatement,statement,o);
    }

    @Override
    public void processAfter(Executor executor, MappedStatement mappedStatement, Statement statement, Object o) {
        this.generator.processAfter(executor,mappedStatement,statement,o);
    }
}
